package avnf_large.delayPercent;

import com.net2plan.interfaces.networkDesign.*;
import com.net2plan.libraries.GraphUtils;
import sun.nio.ch.Net;

import java.io.File;
import java.util.*;

/**
 * @author shentianyu
 * @date 2021-03-22 21:00
 */

/**
 * 计算任意两个节点之间的最短路径的平均距离
 */
public class NodeDistance {
    public static void main(String[] args) {
        String ch = "/Users/shentianyu/Desktop/vnf/networkTopologies/US_Backbone.n2p";
        File f1 = new File(ch);
        NetPlan netPlan = new NetPlan(f1);

        List<Node> nodeList = netPlan.getNodes();
        List<Double> lengthList = new ArrayList<>();
        int numberOfNodes = netPlan.getNumberOfNodes();

        for(int i=0;i<numberOfNodes-1;i++){
            Node source = nodeList.get(i);
            Node destination =nodeList.get(i+1);
            double distance = nodesDistance(netPlan,source,destination);
            lengthList.add(distance);
        }
        System.out.println(avg(lengthList));
    }

    public static double nodesDistance(NetPlan netPlan,Node source, Node destination){

        double distance = 0.0;
        Map<Link,Double> linkCostMap = new HashMap<>();
        for(Link link:netPlan.getLinks()){
            linkCostMap.put(link,link.getLengthInKm());
        }
        List<List<Link>> kShortestPathList = GraphUtils.getKLooplessShortestPaths(netPlan.getNodes(),netPlan.getLinks(),source,destination,linkCostMap,1,Double.MAX_VALUE, Integer.MAX_VALUE,Double.MAX_VALUE,Double.MAX_VALUE,Double.MAX_VALUE,Double.MAX_VALUE);
        List<Link> shortestPath = kShortestPathList.get(0);
        for(Link link:shortestPath){
            distance+=link.getLengthInKm();
        }
        return distance;

    }

    public static double avg(List<Double> lengthList){
        double sum = 0.0;
        int num = lengthList.size();
        for(Double d:lengthList){
            sum+=d;
        }
        double val = sum/(num)*1.0;
        return val;
    }
}
